//
//  TopicWebViewController.m
//  HappyTrip
//
//  Created by dlios on 15-3-26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TopicWebViewController.h"


@interface TopicWebViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIAlertView *alertView;
@property (nonatomic, retain) NSURL *urlStr;

@property (strong,nonatomic)NSString *currentURL;
@property (strong,nonatomic)NSString *currentTitle;
@property (strong,nonatomic)NSString *currentHTML;

@end

@implementation TopicWebViewController

//-(void) webViewDidFinishLoad:(UIWebView *)webView {
//    
//    
//    self.currentURL = webView.request.URL.absoluteString;
//    NSLog(@"title-%@--url-%@--",self.title,self.currentURL);
//    
//    NSString *lJs = @"document.documentElement.innerHTML";//获取当前网页的html
//    self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
//    if (self.currentURL.length != self.url.length) {
//        
//       
//        self.urlStr = [NSURL URLWithString:self.url];
//        NSURLRequest *request =[NSURLRequest requestWithURL:self.urlStr];
//        [self.view addSubview: self.webView];
//        [self.webView loadRequest:request];
////        self.webView.exclusiveTouch = NO;
//        NSLog(@"self.webView %@", self.url);
//        
//    }
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.name;

    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -64, self.view.bounds.size.width, self.view.bounds.size.height + 10)];
    self.urlStr = [NSURL URLWithString:self.url];
    NSURLRequest *request =[NSURLRequest requestWithURL:self.urlStr];
    self.webView.delegate = self;
    [self.view addSubview: self.webView];
    [self.webView loadRequest:request];
    self.webView.exclusiveTouch = NO;
    NSLog(@"self.webView %@", self.url);
    
    // 顶部回弹
    self.webView.scrollView.bounces = NO;
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.webView addGestureRecognizer:swipe];
    [swipe release];
    [_webView release];

    
    
//    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width,49)];
//    [self.view addSubview:label1];
//    [label1 bringSubviewToFront:self.webView];
//    [label1 release];
}
// 判断webView里面的按钮能不能点
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//    NSString *str = [self.url substringWithRange:NSMakeRange(40, 5)];
//    if ([str isEqualToString:@"topic"]) {
        NSLog(@"request:%@",[[request URL] absoluteString]);
        if ([self.url isEqualToString:[[request URL] absoluteString]]) {
            return YES;
        }
//    }
    return NO;

}

//// 验证方法
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    //判断是否是单击
////    if (navigationType == UIWebViewNavigationTypeLinkClicked)
////    {
////        NSURL *url = [request URL];
////        NSLog(@"asda ");
////        if([[UIApplication sharedApplication]canOpenURL:url])
////        {
////            [[UIApplication sharedApplication]openURL:url];
////        }
////        return NO;
////    }
//    if (navigationType == UIWebViewNavigationTypeOther) {
//        if ([webView.request.URL.absoluteString isEqualToString:self.url]) {
//            NSLog(@"7899");
//            NSLog(@"++++++++++%@", webView.request.URL.absoluteString);
//            return NO;
//        }
//    NSLog(@"12313");
//        NSLog(@"——————————%@", request.URL.absoluteString);
//    }
//    return YES;
//}

- (void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
    //    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    // 菊花
    self.MBProgress = [[MBProgressHUD alloc] initWithView:self.view];
    self.MBProgress.labelText = @"加载中...";
    self.MBProgress.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:self.MBProgress];
    [self.MBProgress show:YES];
    [_MBProgress release];

//    [self webViewDidFinishLoad:self.webView];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self.MBProgress removeFromSuperview];
    if (self.webView == nil) {
        self.alertView = [[UIAlertView alloc] initWithTitle:@"加载失败" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [self.alertView show];
        [_alertView release];
        [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:YES];
    }
}
- (void)dismissAlertView
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)dealloc
{
    [_webView release];
    [super dealloc];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
